{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# torchvision.datasets\n",
    "torchvision.datasets 中包含了以下数据集  \n",
    "- MNIST COCO（用于图像标注和目标检测）(Captioning and Detection)   \n",
    "- LSUN Classication \n",
    "- ImageFolder \n",
    "- Imagenet-12 \n",
    "- CIFAR10 \n",
    "- CIFAR100 \n",
    "- STL10"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Datasets 拥有以下 API : \\_\\_getitem__ \\_\\_len__\n",
    "由于以上 Datasets 都是 torch.utils.data.Dataset 的子类，所以，他们也可以通过 torch.utils.data.DataLoader 使用多线程（python的多进程）。  \n",
    "举例说明： torch.utils.data.DataLoader(coco_cap, batch_size=args.batchSize, shuffle=True, num_workers=args.nThreads)在构造函数中，不同的数据集直接的构造函数会有些许不同，但是他们共同拥有 keyword 参数。\n",
    "- transform - 一个函数，原始图片作为输入，返回一个转换后的图片。 （详情请看下面关于 torchvision-tranform 的部分）\n",
    "- target_transform - 一个函数，输入为 target ，输出对其的转换。例子，输入的是图片标注 的 string ，输出为 word 的索引。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "```python\n",
    "import torchvision.datasets as dset\n",
    "import torchvision.transforms as transforms\n",
    "cap = dset.CocoCaptions(root = 'dir where images are',   \n",
    "                        annFile = 'json annotation file',\n",
    "                        transform=transforms.ToTensor())\n",
    "\n",
    "print('Number of samples: ', len(cap))\n",
    "img, target = cap[3] # load 4th sample\n",
    "\n",
    "print(\"Image Size: \", img.size())\n",
    "print(target)\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "```python\n",
    "#检测:\n",
    "dset.CocoDetection(root=\"dir where images are\", annFile=\"json annotation file\", [transform, target_transform])  #需要路径\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## LSUN\n",
    "dset.LSUN(db_path, classes='train', [transform, target_transform])  \n",
    "参数说明： \n",
    "- db_path = 数据集文件的根目录 \n",
    "- classes = ‘train’ (所有类别, 训练集), ‘val’ (所有类别, 验证集), ‘test’ (所有类别, 测试集)\n",
    "- [‘bedroom_train’, ‘church_train’, …] - 需要加载的类别列"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## ImageFolder\n",
    "一个通用的数据加载器，数据集中的数据以以下方式组织：  \n",
    "root/dog/xxx.png、root/dog/xxy.png、root/dog/xxz.png  \n",
    "root/cat/123.png、root/cat/nsdf3.png、root/cat/asd932_.png    \n",
    "\n",
    "dset.ImageFolder(root=\"root folder path\", [transform, target_transform])  \n",
    "有以下成员变量:  \n",
    "- self.classes - 用一个list保存 类名\n",
    "- self.class_to_idx - 类名对应的 索引\n",
    "- self.imgs - 保存(img-path, class) tuple的list\n",
    "```python\n",
    "img_dir='.\\\\img'\n",
    "#img_dir='E:\\\\mtcnn-onet\\\\train'\n",
    "data = ImageFolder(img_dir,transform=transforms.Compose(\n",
    "    [transforms.Resize(224), #Bnnet 改成64,mobilenet,resnet改成224\n",
    "     transforms.RandomHorizontalFlip(),\n",
    "     transforms.ToTensor(),\n",
    "     #transforms.Normalize(mean=(0.034528155, 0.033598177, 0.009853649), std=(0.15804708, 0.16410254, 0.0643605))\n",
    "     ]))\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## CIFAR\n",
    "dset.CIFAR10(root, train=True, transform=None, target_transform=None, download=False)  \n",
    "dset.CIFAR100(root, train=True, transform=None, target_transform=None, download=False)  \n",
    "参数说明： \n",
    "- root : cifar-10-batches-py 的根目录 \n",
    "- train : True = 训练集, False = 测试集 - \n",
    "- download : True = 从互联上下载数据，并将其放在root目录下"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## STL10\n",
    "dset.STL10(root, split='train', transform=None, target_transform=None, download=False)  \n",
    "参数说明：   \n",
    "- root : stl10_binary的根目录 \n",
    "- split : 'train' = 训练集, 'test' = 测试集, 'unlabeled' = 无标签数据集, 'train+unlabeled' = 训练 + 无标签数据集 (没有标签的标记为-1) \n",
    "- download : True = 从互联上下载数据，并将其放在root目录下"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.2"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {
    "height": "calc(100% - 180px)",
    "left": "10px",
    "top": "150px",
    "width": "273.188px"
   },
   "toc_section_display": true,
   "toc_window_display": true
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
